package core

import (
	influxdb2 "github.com/influxdata/influxdb-client-go/v2"
	"github.com/influxdata/influxdb-client-go/v2/api"
	"seven-admin/core/config"
)

var influxDBClient = initInfluxDB()

func GetInfluxDB() influxdb2.Client {
	return influxDBClient
}

// query influxdb
func QueryInflux() api.QueryAPI {
	return influxDBClient.QueryAPI(config.Env.INFLUXDB_ORGANIZATION)
}

// write influxdb
func WriteInflux() api.WriteAPIBlocking {
	return influxDBClient.WriteAPIBlocking(config.Env.INFLUXDB_ORGANIZATION, config.Env.INFLUXDB_BUCKET)
}

func CloseInfluxDB() {
	influxDBClient.Close()
}

func initInfluxDB() influxdb2.Client {
	return influxdb2.NewClient(config.Env.INFLUXDB_URL, config.Env.INFLUXDB_TOKEN)
}
